<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.11.0"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>cppMISC: strategy 策略模式</title>
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../clipboard.js"></script>
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../resize.js"></script>
<script type="text/javascript" src="../../cookie.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/javascript" src="../../darkmode_toggle.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr id="projectrow">
  <td id="projectalign">
   <div id="projectname">cppMISC<span id="projectnumber">&#160;v1.1</span>
   </div>
   <div id="projectbrief">cppMisc 文档系统</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- 制作者 Doxygen 1.11.0 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "../../search/",'.html');
/* @license-end */
</script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() { codefold.init(1); });
/* @license-end */
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
  initMenu('../../',true,false,'search.php','搜索',false);
  $(function() { init_search(); });
});
/* @license-end */
</script>
<div id="main-nav"></div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function(){ initResizable(false); });
/* @license-end */
</script>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">载入中...</div>
<div class="SRStatus" id="Searching">搜索中...</div>
<div class="SRStatus" id="NoMatches">未找到</div>
</div>
</div>
</div>
</div>

</div><!-- top -->
<div id="doc-content">
<div><div class="header">
  <div class="headertitle"><div class="title">strategy 策略模式</div></div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><a class="anchor" id="autotoc_md4"></a></p>
<h3><a class="anchor" id="autotoc_md5"></a>
实现原理</h3>
<ul>
<li>1 利用 std::map , std::function 存储策略方法</li>
<li>2 统一调用接口, 使用call根据条件变量调用对应的方法</li>
<li>3 支持任意类型的返回值</li>
<li>4 纯模板实现，直接包含使用 </li>
</ul>
<h3><a class="anchor" id="autotoc_md6"></a>
主要功能</h3>
<ul>
<li>支持任意条件变量</li>
<li>支持任意个参数传递</li>
</ul>
<h3><a class="anchor" id="autotoc_md7"></a>
示例</h3>
<div class="fragment"><div class="line">#include &lt;vector&gt;</div>
<div class="line">#include &lt;iostream&gt;</div>
<div class="line"> </div>
<div class="line">#include &quot;designM/strategy.hpp&quot;</div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line">using namespace wheels;</div>
<div class="line">using namespace dm;</div>
<div class="line"> </div>
<div class="line">int fun1( int param , int param2)</div>
<div class="line">{</div>
<div class="line">    std::cout &lt;&lt; &quot;fun1 called&quot; &lt;&lt; std::endl;</div>
<div class="line">    return param + param2;</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line">int fun2( int param , int param2)</div>
<div class="line">{</div>
<div class="line">    std::cout &lt;&lt; &quot;fun2 called&quot; &lt;&lt; std::endl;</div>
<div class="line">    return param * param2;</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line">int main( void )</div>
<div class="line">{</div>
<div class="line">        // 构造策略对象，模板参数的含义：条件变量类型，返回值类型，方法参数表</div>
<div class="line">    strategy&lt; int , int , int , int &gt;  strat;</div>
<div class="line">    // 调用add添加方法</div>
<div class="line">    strat.add( 0 , []( int param , int param2)-&gt;int{</div>
<div class="line">        return fun1( param , param2);</div>
<div class="line">    });</div>
<div class="line">    </div>
<div class="line">    strat.add( 1 , []( int param , int param2)-&gt;int{</div>
<div class="line">        return fun2( param , param2);</div>
<div class="line">    });</div>
<div class="line">    // 使用策略对象的call方法调用实际的具体方法</div>
<div class="line">    std::cout &lt;&lt; strat.call( 0 , 12 , 12) &lt;&lt; std::endl;</div>
<div class="line">    std::cout &lt;&lt; strat.call( 1 , 78 , 12) &lt;&lt; std::endl;</div>
<div class="line"> </div>
<div class="line">    return 0;</div>
<div class="line">}</div>
</div><!-- fragment --> </div></div><!-- contents -->
</div><!-- PageDoc -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
制作者&#160;<a href="https://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.11.0
</small></address>
</div><!-- doc-content -->
</body>
</html>
